package com.zjp.dataFormat.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.zjp.dataFormat.annotation.RangeTime;
import com.zjp.dataFormat.enums.UserFormatEnum;
import com.zjp.dataFormat.pojo.common.CommonTimeQuery;
import com.zjp.dataFormat.pojo.user.AbstractUser;
import com.zjp.dataFormat.pojo.user.BaseUser;
import com.zjp.dataFormat.pojo.user.SimpleUser;
import com.zjp.dataFormat.pojo.user.UserInfo;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserService {

    private UserFormatEnum formatEnum;

    public Map<Long, SimpleUser> simpleUserMap() {
        List<String> stringList = Arrays.asList("多多", "敏敏", "康康", "鹏鹏", "波波");
        Map<Long, SimpleUser> map = new HashMap<>();
        for (int i = 1; i <= 10; i++) {
            SimpleUser user = new SimpleUser();
            Long userId = Convert.toLong(i);
            user.setUserId(userId);
            user.setNickName(RandomUtil.randomEle(stringList));
            map.put(userId, user);
        }
        return map;
    }

    public Map<Long, UserInfo> userInfoMap() {
        List<String> stringList = Arrays.asList("多多", "敏敏", "康康", "鹏鹏", "波波");
        Map<Long, UserInfo> map = new HashMap<>();
        for (int i = 1; i <= 10; i++) {
            UserInfo user = new UserInfo();
            Long userId = Convert.toLong(i);
            user.setUserId(userId);
            String s = RandomUtil.randomEle(stringList);
            user.setNickName(s);
            user.setUserName(s);
            map.put(userId, user);
        }
        return map;
    }

    public Map<Long, ? extends AbstractUser> userMap(UserFormatEnum formatEnum, Collection<Long> collection) {
        if (formatEnum.equals(UserFormatEnum.INFO)) {
            return userInfoMap();
        } else if (formatEnum.equals(UserFormatEnum.SIMPLE)) {
            return simpleUserMap();
        }
        return Collections.emptyMap();
    }

    public <T extends Collection<E>, E extends BaseUser> T format(T collection, Collection<Long> ids, UserFormatEnum formatEnum) {
        Map<Long, ? extends AbstractUser> map = userMap(formatEnum, ids);
        for (BaseUser baseUser : collection) {
            Long userId = baseUser.getUserId();
            if (map.containsKey(userId)) {
                baseUser.setUser(map.get(userId));
            }
        }
        return collection;
    }

    public void format2(Collection<? extends BaseUser> collection, Collection<Long> ids, UserFormatEnum formatEnum) {
        Map<Long, ? extends AbstractUser> map = userMap(formatEnum, ids);
        for (BaseUser baseUser : collection) {
            Long userId = baseUser.getUserId();
            if (map.containsKey(userId)) {
                baseUser.setUser(map.get(userId));
            }
        }
    }

    @RangeTime
    public void formatTime(CommonTimeQuery query) {

    }


}